
use proto_lib;
#[cfg(test)]
mod tests {
    use std::env;
    use std::fs::File;
    use std::io::Read;
    use prost::Message;
    use prost_types::FileDescriptorSet;

    const FILE_NAME: &str = "../proto-lib/proto/descriptor.bin";
    fn load_descriptor() -> Result<FileDescriptorSet, Box<dyn std::error::Error>> {
        // 获取当前工作目录
        let current_dir = env::current_dir()?;
        println!("Current dir: {:?}", current_dir); // Current dir: "D:\\workspace\\rs-exchange\\crates\\gateway"

        // 获取可执行文件目录
        let exe_dir = env::current_exe()?;
        println!("Executable dir: {:?}", exe_dir); // Executable dir: "D:\\workspace\\rs-exchange\\target\\debug\\deps\\test_descriptor-7f02c0024fa88dc9.exe"
        // 读取描述符文件
        let mut file = File::open(FILE_NAME)?;
        let mut buffer = Vec::new();
        file.read_to_end(&mut buffer)?;

        // 解析描述符
        let descriptor_set = FileDescriptorSet::decode(&buffer[..])?;
        Ok(descriptor_set)
    }

    fn print_service_info(descriptor_set: &FileDescriptorSet) {
        for file in &descriptor_set.file {
            println!("File: {}", file.name.as_ref().unwrap_or(&String::new()));

            // 打印服务信息
            for service in &file.service {
                println!("  Service: {}", service.name.as_ref().unwrap_or(&String::new()));

                // 打印方法信息
                for method in &service.method {
                    println!("    Method: {}", method.name.as_ref().unwrap_or(&String::new()));
                    println!("      Input: {}", method.input_type.as_ref().unwrap_or(&String::new()));
                    println!("      Output: {}", method.output_type.as_ref().unwrap_or(&String::new()));
                }
            }
        }
    }

    #[test]
    fn test_descriptor() {
        let fd = load_descriptor().unwrap();
        print_service_info(&fd);
    }
}